package com.company.project.temp;

import com.mongodb.BasicDBObject;
import com.mongodb.Block;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;

import java.util.*;
import java.util.regex.Pattern;

/**
 * Hello world!
 *
 */
public class SystemStatus {

	private static Map<String, Integer> map = new HashMap();

	public static void main(String[] args) {
		// Set<String> machines = getMachines();

		List<Machine> accidMachineInfos = getAccidMachineInfos();
		for (Machine accidMachineInfo : accidMachineInfos) {

			String machineCode = accidMachineInfo.getMachineCode();
			String accid = accidMachineInfo.getAccid();

//			if (machines.contains(machineCode)) {
//				System.out.println("'" + machineCode + "',-" + accid);
//			}
		}

		System.out.println("++++++++++++++++");
		for (Machine accidMachineInfo : accidMachineInfos) {

			String machineCode = accidMachineInfo.getMachineCode();
			String accid = accidMachineInfo.getAccid();
			String networkOperateName = accidMachineInfo.getNetworkOperateName();

			//if (machines.contains(machineCode)) { // network_operate_name
			if (StringUtils.isEmpty(machineCode) || machineCode.equals("null") || StringUtils.isEmpty(accid) || accid.equals("null")) {
				continue;
			}

				String test = "insert into temp_accid (machine_code,accid,network_operate_name) VALUES ('"+machineCode+"', '"+accid+"', '"+networkOperateName+"');";
				System.out.println(test);
			//}

		}

	}


	private static BasicDBObject getLikeStr(String findStr) {
		Pattern pattern = Pattern.compile("^.*" + findStr + ".*$", Pattern.CASE_INSENSITIVE);
		return new BasicDBObject("$regex", pattern);
	}


	private static List<Machine> getAccidMachineInfos() {

		List<Machine> machines = new ArrayList<>();

		MongoClient mongo = new MongoClient("pre_test.72solo.com", 27017);
		MongoDatabase db = mongo.getDatabase("inno72_prod_12031554");
		MongoCollection<Document> collection = db.getCollection("SystemStatus");
		BasicDBObject query = new BasicDBObject();

		collection.find(query).forEach(new Block<Document>() {

			@Override
			public void apply(Document next) {
				String machineId = (String) next.get("machineId");
				String accid = (String) next.get("accid");
				String networkOperateName = (String) next.get("networkOperateName");

				Machine machine = new Machine();
				machine.setMachineCode(machineId);
				machine.setAccid(accid);
				machine.setNetworkOperateName(networkOperateName);
				machines.add(machine);
			}

		});

		mongo.close();
		return machines;
	}

	private static Set<String> getMachines() {

		Set<String> machineIds = new HashSet<>();

		MongoClient mongo = new MongoClient("pre_test.72solo.com", 27017);
		MongoDatabase db = mongo.getDatabase("inno72_prod_12031554");
		MongoCollection<Document> collection = db.getCollection("PointLog");
		BasicDBObject query = new BasicDBObject();
		query.put("tag", getLikeStr("\"sdFree\":0"));

		collection.find(query).forEach(new Block<Document>() {

			@Override
			public void apply(Document next) {
				String tag = (String) next.get("tag");

				if (StringUtils.isNotEmpty(tag)) {
					try {
						String accid = FastJsonUtils.getString(tag, "accid");
						String sdFree = FastJsonUtils.getString(tag, "sdFree");
						String machineId = FastJsonUtils.getString(tag, "machineId");
						if (StringUtils.isNotEmpty(sdFree) && sdFree.equals("0")) {
							System.out.println(sdFree + " " + machineId);
							machineIds.add(machineId);
						}
					} catch (Exception e) {
						// e.printStackTrace();
					}
				}
			}

		});

		System.out.println("++++++++++++++++");
		for (String machineId : machineIds) {
			System.out.println(machineId);
		}

		mongo.close();
		return machineIds;

	}

}
